package com.scalar.chiptrack.operations.dataentry.boardInventory.actioncommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.operations.dataentry.boardInventory.forms.BoardInventoryDataEntryForm;
import com.scalar.chiptrack.operations.dataentry.LotDetails;
import com.scalar.chiptrack.operations.dataentry.dao.DataEntryDAO;
import com.scalar.chiptrack.operations.process.Lot;
import com.scalar.chiptrack.operations.process.VendorLocation;
import com.scalar.chiptrack.operations.process.dao.PartNumber;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.commons.SessionKeys;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * User: ganesh
 * Date: Sep 3, 2007
 * Time: 5:07:28 PM

 */
public class BoardInventoryDataEntryLoadFabLotNumbersCommand extends ActionCommand {

    private static final String ACTION_NAME = "/boardInvDataEntry.do" ;

    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception
    {
        BoardInventoryDataEntryForm boardInvDataEntryForm = (BoardInventoryDataEntryForm) form;

        HashMap errors = new HashMap();
        HashMap info = new HashMap();
        String action = request.getParameter("action");

        request.setAttribute(INFO_MESSAGES, info);
        request.setAttribute(ERROR_MESSAGES, errors);


        HttpSession mSession = request.getSession();
        User user = (User) mSession.getAttribute(SessionKeys.USER_KEY);

        if (user == null)
        {
            mSession.setAttribute(SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME);
            return (mapping.findForward("login"));
        }
        else
        {
            if ( user.isActionAllowed( Functionality.DATA_ENTRY, User.USER_ACTION_MODIFY ) )
            {
                request.setAttribute("showMessage", "FALSE");
            }
            else if ( user.isActionAllowed( Functionality.BOARD_INVENTORY_DATA_ENTRY , User.USER_ACTION_MODIFY ) )
            {
                request.setAttribute("showMessage", "FALSE");
            }
            else
            {
                request.setAttribute("showMessage", "TRUE");
            }
        }
        ArrayList partNumberList;
        ArrayList locationList;
        ArrayList processTypeList;
        ArrayList fabLotNumberList;
        Lot lot;
        PartNumber partNumber;
        VendorLocation location;
        LotDetails lotDetails = new LotDetails();

        ArrayList invPartNumbersLableValueBeans     = new ArrayList();
        ArrayList invLocationsLableValueBeans       = new ArrayList();
        ArrayList invfabLotNumberLabelValueBeans    = new ArrayList();

        try
        {
            processTypeList = lotDetails.getBoardInventoryProcessList();

            partNumberList = DataEntryDAO.getBoardInvPartNumber(boardInvDataEntryForm.getProcessType());
            for (int i = 0; i < partNumberList.size(); i++)
            {
                partNumber = (PartNumber) partNumberList.get(i);
                invPartNumbersLableValueBeans.add(new org.apache.struts.util.LabelValueBean(partNumber.getPartNumber(), partNumber.getProcessStepId()));
            }
            locationList= VendorDAO.getInventoryLocations(boardInvDataEntryForm.getProcessType());
            //locationList = DataEntryDAO.getSelectedLocations(Integer.parseInt(invDataEntryForm.getProcessStepId()));
            for (int i = 0; i < locationList.size(); i++)
            {
                location = (VendorLocation) locationList.get(i);
                invLocationsLableValueBeans.add(new org.apache.struts.util.LabelValueBean(location.getLocationName(), location.getLocationID()));
            }

            fabLotNumberList = DataEntryDAO.getRunningFabLotNumbersByPSTypeName(boardInvDataEntryForm.getProcessStepId(), boardInvDataEntryForm.getLocationId());
            for (int i = 0; i < fabLotNumberList.size(); i++)
            {
                lot = (Lot) fabLotNumberList.get(i);
                invfabLotNumberLabelValueBeans.add(new org.apache.struts.util.LabelValueBean(lot.getFabLotnumber(), lot.getFabLotnumber()));
            }

            boardInvDataEntryForm.setLotId("0");
            request.setAttribute("processTypeList", processTypeList);
            request.setAttribute("partNumbersList", invPartNumbersLableValueBeans);
            request.setAttribute("locationList", invLocationsLableValueBeans);
            request.setAttribute("invFabLotNoList", invfabLotNumberLabelValueBeans);
            request.setAttribute("action", action);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return mapping.findForward("boardInvDataEntry");
    }
}
